*Write a permanent log file;
PROC PRINTTO LOG="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P03 ED Compute rates.log" NEW;
RUN;

*Write a permanent list file;
PROC PRINTTO PRINT="C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P03 ED Compute rates.lst" NEW;
RUN;

/*
Combine the counts of Emergency Department treatments with the population denominators to estimate rates
*/

*Create age profiles by group;
%macro b_group(o_file,suf,a_where);
proc means data = inter.rep_alcohol noprint;
   var  visit alcohol_any injury inj_by_oth inj_by_self inj_accident illness injury_or_alc;
   output out = &o_file. sum = visit_&suf. alcohol_any_&suf. injury_&suf. inj_by_oth_&suf. inj_by_self_&suf. inj_accident_&suf. illness_&suf. injury_or_alc_&suf.;
   class months_21;
   where state in ('AZ','NJ','WI') &a_where.;
run;
%mend;
%b_group(all,all,);*Create overall age profile in counts;
%b_group(all_np,all_np,and preg_related = 0);*Create age profile for all non pregnant;
%b_group(female,f,and female = 1);*Create age profile for women;
%b_group(female_np,f_np,and female = 1 and preg_related = 0);*Create age profile for women not in for pregnancy;
%b_group(male,m,and female =  0);*Create age profile for men;

*By state;
%macro by_st(ST);
proc means data = inter.rep_alcohol noprint;
   var  visit alcohol_any injury inj_by_oth inj_by_self inj_accident illness injury_or_alc;
   output out = all_&ST. sum = visit_&ST._NP alcohol_any_&ST._NP injury_&ST._NP inj_by_oth_&ST._NP inj_by_self_&ST._NP inj_accident_&ST._NP illness_&ST._NP injury_or_alc_&ST._NP;
   class months_21;
   where state = "&ST."  and preg_related = 0;
run;
%mend;
%by_st(AZ);
%by_st(CA);
%by_st(IA);
%by_st(NJ);
%by_st(WI);

*Combine all the counts of treatments;
data counts (drop = _type_ _freq_);
   merge all all_np male female female_np all_AZ all_CA all_IA all_NJ all_WI;
   by months_21;
  *Drop the total count generated by the proc means;
   if months_21 = . then delete;
   merge_v = 1;
run;

*Compute the rates;
data inter.alc_rates (keep = months_21 age bmonth post linear linear_post age_months age_c age_c_sq over age_c_post age_c_post_sq dummy21 
		visit_all_r 	alcohol_any_all_r 		injury_all_r 	inj_by_self_all_r 		inj_by_oth_all_r 	inj_accident_all_r 	 	illness_all_r 		injury_or_alc_all_r
		visit_all_np_r 	alcohol_any_all_np_r 	injury_all_np_r inj_by_self_all_np_r 	inj_by_oth_all_np_r inj_accident_all_np_r  	illness_all_np_r 	injury_or_alc_all_np_r
		visit_m_r 		alcohol_any_m_r 		injury_m_r 		inj_by_self_m_r 		inj_by_oth_m_r 		inj_accident_m_r 	 	illness_m_r			injury_or_alc_m_r
		visit_f_r 		alcohol_any_f_r 		injury_f_r 		inj_by_self_f_r 		inj_by_oth_f_r 		inj_accident_f_r 	 	illness_f_r			injury_or_alc_f_r
		visit_f_np_r	alcohol_any_f_np_r 		injury_f_np_r 	inj_by_self_f_np_r 		inj_by_oth_f_np_r 	inj_accident_f_np_r  	illness_f_np_r		injury_or_alc_f_np_r
		visit_AZ_NP_r	alcohol_any_AZ_NP_r		injury_AZ_NP_r	iNJ_by_self_AZ_NP_r		iNJ_by_oth_AZ_NP_r	iNJ_accident_AZ_NP_r	illness_AZ_NP_r		injury_or_alc_AZ_NP_r
		visit_CA_NP_r	alcohol_any_CA_NP_r		injury_CA_NP_r	iNJ_by_self_CA_NP_r		iNJ_by_oth_CA_NP_r	iNJ_accident_CA_NP_r	illness_CA_NP_r		injury_or_alc_CA_NP_r
		visit_IA_NP_r	alcohol_any_IA_NP_r		injury_IA_NP_r	iNJ_by_self_IA_NP_r		iNJ_by_oth_IA_NP_r	iNJ_accident_IA_NP_r	illness_IA_NP_r		injury_or_alc_IA_NP_r
		visit_NJ_NP_r	alcohol_any_NJ_NP_r		injury_NJ_NP_r	iNJ_by_self_NJ_NP_r		iNJ_by_oth_NJ_NP_r	iNJ_accident_NJ_NP_r	illness_NJ_NP_r		injury_or_alc_NJ_NP_r
		visit_WI_NP_r	alcohol_any_WI_NP_r		injury_WI_NP_r	iNJ_by_self_WI_NP_r		iNJ_by_oth_WI_NP_r	iNJ_accident_WI_NP_r	illness_WI_NP_r    	injury_or_alc_WI_NP_r);
   merge counts inter.alc_pop_combined;
   by merge_v;
  *Compute the age variable;
   age = 21+months_21/12;
  *Compute the running variable and month dummy;
   if months_21 = 0 then bmonth = 1; else bmonth = 0;
   if months_21 >=0 then post = 1; else post = 0;
   linear = months_21;
   linear_post = months_21*post;
  *Arrays for all;
   array all{8} 		visit_all 		alcohol_any_all 		injury_all 			inj_by_self_all 		inj_by_oth_all 		inj_accident_all  		illness_all			injury_or_alc_all;
   array all_r{8} 		visit_all_r 	alcohol_any_all_r 		injury_all_r 		inj_by_self_all_r 		inj_by_oth_all_r 	inj_accident_all_r  	illness_all_r		injury_or_alc_all_r;
  *Arrays for all not pregnant;
   array all_np{8} 		visit_all_np 	alcohol_any_all_np 		injury_all_np 		inj_by_self_all_np 		inj_by_oth_all_np 	inj_accident_all_np  	illness_all_np		injury_or_alc_all_np;
   array all_np_r{8} 	visit_all_np_r 	alcohol_any_all_np_r 	injury_all_np_r 	inj_by_self_all_np_r 	inj_by_oth_all_np_r inj_accident_all_np_r  	illness_all_np_r	injury_or_alc_all_np_r;
  *Arrays by gender;
   array male{8} 		visit_m 		alcohol_any_m 		injury_m 		inj_by_self_m 		inj_by_oth_m 		inj_accident_m 	 	illness_m		injury_or_alc_m;
   array male_r{8} 		visit_m_r 		alcohol_any_m_r 	injury_m_r 		inj_by_self_m_r 	inj_by_oth_m_r 		inj_accident_m_r  	illness_m_r		injury_or_alc_m_r;
   array female{8} 		visit_f 		alcohol_any_f 		injury_f 		inj_by_self_f 		inj_by_oth_f 		inj_accident_f  	illness_f		injury_or_alc_f;
   array female_r{8} 	visit_f_r 		alcohol_any_f_r 	injury_f_r 		inj_by_self_f_r 	inj_by_oth_f_r 		inj_accident_f_r  	illness_f_r		injury_or_alc_f_r;
   array female_np{8} 	visit_f_np 		alcohol_any_f_np 	injury_f_np	 	inj_by_self_f_np 	inj_by_oth_f_np 	inj_accident_f_np  	illness_f_np	injury_or_alc_f_np;
   array female_np_r{8} visit_f_np_r 	alcohol_any_f_np_r 	injury_f_np_r 	inj_by_self_f_np_r 	inj_by_oth_f_np_r 	inj_accident_f_np_r	illness_f_np_r	injury_or_alc_f_np_r;
  *Arrays by race;
   array AZ{8} visit_AZ_NP alcohol_any_AZ_NP injury_AZ_NP inj_by_self_AZ_NP inj_by_oth_AZ_NP inj_accident_AZ_NP illness_AZ_NP	injury_or_alc_AZ_NP;
   array CA{8} visit_CA_NP alcohol_any_CA_NP injury_CA_NP inj_by_self_CA_NP inj_by_oth_CA_NP inj_accident_CA_NP illness_CA_NP	injury_or_alc_CA_NP;
   array IA{8} visit_IA_NP alcohol_any_IA_NP injury_IA_NP inj_by_self_IA_NP inj_by_oth_IA_NP inj_accident_IA_NP illness_IA_NP	injury_or_alc_IA_NP;
   array NJ{8} visit_NJ_NP alcohol_any_NJ_NP injury_NJ_NP inj_by_self_NJ_NP inj_by_oth_NJ_NP inj_accident_NJ_NP illness_NJ_NP	injury_or_alc_NJ_NP;
   array WI{8} visit_WI_NP alcohol_any_WI_NP injury_WI_NP inj_by_self_WI_NP inj_by_oth_WI_NP inj_accident_WI_NP illness_WI_NP	injury_or_alc_WI_NP;
   array AZ_r{8} visit_AZ_NP_r alcohol_any_AZ_NP_r injury_AZ_NP_r iNJ_by_self_AZ_NP_r iNJ_by_oth_AZ_NP_r iNJ_accident_AZ_NP_r illness_AZ_NP_r	injury_or_alc_AZ_NP_r;
   array CA_r{8} visit_CA_NP_r alcohol_any_CA_NP_r injury_CA_NP_r iNJ_by_self_CA_NP_r iNJ_by_oth_CA_NP_r iNJ_accident_CA_NP_r illness_CA_NP_r	injury_or_alc_CA_NP_r;
   array IA_r{8} visit_IA_NP_r alcohol_any_IA_NP_r injury_IA_NP_r iNJ_by_self_IA_NP_r iNJ_by_oth_IA_NP_r iNJ_accident_IA_NP_r illness_IA_NP_r	injury_or_alc_IA_NP_r;
   array NJ_r{8} visit_NJ_NP_r alcohol_any_NJ_NP_r injury_NJ_NP_r iNJ_by_self_NJ_NP_r iNJ_by_oth_NJ_NP_r iNJ_accident_NJ_NP_r illness_NJ_NP_r	injury_or_alc_NJ_NP_r;
   array WI_r{8} visit_WI_NP_r alcohol_any_WI_NP_r injury_WI_NP_r iNJ_by_self_WI_NP_r iNJ_by_oth_WI_NP_r iNJ_accident_WI_NP_r illness_WI_NP_r	injury_or_alc_WI_NP_r;
  *Compute rates;
   do i = 1 to 8;
      all_r{i} = 10000*all{i}/pop_all_AZ_NJ_WI;
	  all_np_r{i} = 10000*all_np{i}/pop_all_AZ_NJ_WI;
      male_r{i} = 10000*male{i}/pop_male_AZ_NJ_WI;
      female_r{i} = 10000*female{i}/pop_female_AZ_NJ_WI;
      female_np_r{i} = 10000*female_np{i}/pop_female_AZ_NJ_WI;
      AZ_r{i} = 10000*AZ{i}/pop_AZ;
      CA_r{i} = 10000*CA{i}/pop_CA;
      IA_r{i} = 10000*IA{i}/pop_IA;
      NJ_r{i} = 10000*NJ{i}/pop_NJ;
      WI_r{i} = 10000*WI{i}/pop_WI;
   end;
   age_months = months_21/12 +21;    
   age_c = age_months - 21;  
   age_c_sq = age_c*age_c;
   if age_c >= 0 then over = 1; 
   if age_c < 0 then over = 0; 
   age_c_post = over*age_c ;
   age_c_post_sq = age_c_post*age_c_post;  
   if age_months = 21 then dummy21 = 1; else dummy21 = 0; 
run;

*California has a substantially lower rate but it looks right;
proc means data = inter.alc_rates mean;
   var visit_all_NP_r visit_AZ_NP_r visit_CA_NP_r visit_IA_NP_r visit_NJ_NP_r visit_WI_NP_r visit_m_r visit_f_r;
   where months_21 >= -12 and months_21 <=12;
run;

*Output this to a dta file;
PROC EXPORT DATA= inter.alc_rates
            OUTFILE= "C:\Research\Alcohol and Morbidity\7. Code Archive\Data Files and Code That Produced Them\P03 ED Analysis File.dta"
            REPLACE;
RUN;

*Direct list file back to lst window;
PROC PRINTTO PRINT=PRINT;
RUN; 

*Direct log file back to log window;
PROC PRINTTO LOG=LOG;
RUN; 

